﻿using System;
using System.Collections.Generic;
using System.Text;
using RockCompany.Common.Lib;
using RockCompany.Master.DTO;
using RockCompany.Common.Constant;
using System.Windows.Forms;
using System.Data;

namespace RockCompany.Master.Model
{
    public class SI07BFormModel : CommonModel
    {
        #region Properties
        private DataTable _prodImportDetail;
        public DataTable prodImportDetail
        {
            get { return _prodImportDetail; }
            set { _prodImportDetail = value; }
        }
        #endregion

        #region Constructor
        public SI07BFormModel()
        {
            prodImportDetail = new DataTable();
            DataList = new List<object>();
            dbConnector = new DBConnector();
        }
        #endregion

        #region Utility Methods
        public DataRow getCheckedRow(int index)
        {
            DataRow curRow = prodImportDetail.Rows[index];
            curRow.SetField<bool>(PHIEU_XK_TP_CT_DTO.N_CHON, !curRow.Field<bool>(PHIEU_XK_TP_CT_DTO.N_CHON));
            return curRow;
        }

        private DataTable searchWorkOrderDetail(List<string> sWorkOrderList)
        {
            DataTable tempDB = null;
            if (sWorkOrderList.Count > 0)
            {
                string sSql = "SELECT " + CONG_LENH_SX.N_SO_LO + "," + CONG_LENH_SX.N_MA_MAU;
                sSql += " FROM " + CONG_LENH_SX.N_TABLE;
                sSql += " WHERE " + CONG_LENH_SX.N_SO_LO + " IN (";
                foreach (string s in sWorkOrderList)
                {
                    sSql += "'" + s + "',";
                }
                sSql = sSql.Substring(0, sSql.Length - 1) + ")";
                tempDB = selectData(sSql);
            }
            return tempDB;
        }
        #endregion

        #region Main Methods
        public DataTable searchProdImportList(string sNo, string sfromDate, string sToDate)
        {
            string sSql = "SELECT DISTINCT CONVERT(BIT,'0') AS CHON ,";
            sSql += PHIEU_NK_TP_CT_DTO.N_MS_PHIEU_NK + ",";
            sSql += PHIEU_NK_TP_CT_DTO.N_PHE_DUYET + ",";
            sSql += PHIEU_NK_TP_CT_DTO.N_CREATION_USER + ",";
            sSql += "CONVERT(VARCHAR," + PHIEU_NK_TP_CT_DTO.N_CREATION_TIME + ",100) AS " + PHIEU_NK_TP_CT_DTO.N_CREATION_TIME;
            sSql += " FROM " + PHIEU_NK_TP_CT_DTO.N_TABLE;
            sSql += " WHERE " + PHIEU_NK_TP_CT_DTO.N_MS_PHIEU_NK + " LIKE '" + sNo + "%'";
            if (!sfromDate.Equals(CommonConstantIF.BLANK))
            {
                sSql += " AND CONVERT(VARCHAR," + PHIEU_NK_TP_CT_DTO.N_CREATION_TIME + ",112) BETWEEN " + sfromDate + " AND " + sToDate;
            }
            return selectData(sSql);
        }

        public DataTable searchProdImportDetail(string sNo)
        {
            string sSql = "SELECT CONVERT(BIT,'0') AS " + PHIEU_NK_TP_CT_DTO.N_CHON + ",";
            sSql += "NK.*,";
            sSql += CONG_LENH_SX.N_MA_MAU;
            sSql += " FROM " + PHIEU_NK_TP_CT_DTO.N_TABLE + " NK ," + CONG_LENH_SX.N_TABLE + " CL";
            sSql += " WHERE " + PHIEU_NK_TP_CT_DTO.N_MS_PHIEU_NK + " = '" + sNo + "'";
            sSql += " AND NK." + PHIEU_NK_TP_CT_DTO.N_SO_LO + " = CL." + CONG_LENH_SX.N_SO_LO;
            prodImportDetail = selectData(sSql);
            return prodImportDetail;
        }

        public bool removeProdImportDetail(string sProdImportNo)
        {
            string sWhereClause = " WHERE ";
            sWhereClause += PHIEU_NK_TP_CT_DTO.N_MS_PHIEU_NK + " ='" + sProdImportNo + "'";
            sWhereClause += " AND " + PHIEU_NK_TP_CT_DTO.N_SO_LO + " IN(";
            if (!existData(prodImportDetail))
            {
                searchProdImportDetail(sProdImportNo);
            }

            Dictionary<string, object> pv = new Dictionary<string, object>();

            foreach (DataRow row in prodImportDetail.Rows)
            {
                string key = "@" + PHIEU_NK_TP_CT_DTO.N_SO_LO + prodImportDetail.Rows.IndexOf(row).ToString();
                pv.Add(key, Util.parseString(row[PHIEU_NK_TP_CT_DTO.N_SO_LO]));
                sWhereClause += key + ",";
            }
            sWhereClause = sWhereClause.Substring(0, sWhereClause.Length - 1) + ")";
            return removeRow(PHIEU_NK_TP_CT_DTO.N_TABLE, sWhereClause, pv);
        }

        public bool removePhieuXK(string msPhieuXK)
        {
            string sWhereClause = " WHERE ";
            sWhereClause += PHIEU_XK_TP_DTO.N_MS_PHIEU_XK_TP + " IN(";

            sWhereClause += "'" + msPhieuXK + "'";
                
            sWhereClause += ")";

            return removeRow(PHIEU_XK_TP_CT_DTO.N_TABLE, sWhereClause, null) && removeRow(PHIEU_XK_TP_DTO.N_TABLE, sWhereClause, null);
        }

        public bool export2Excel(string newPath, string msPhieu)
        {
            bool b = false;
            if (!CommonConstantIF.BLANK.Equals(newPath))
            {
                DataTable phieu = searchProdImportDetail(msPhieu);
                
                if (CommonModel.existData(phieu))
                {
                    ExcelExporter excelExport = new ExcelExporter();
                    excelExport.Trans = this.translator;

                    string fileName = "SI07";

                    excelExport.open(fileName);

                    excelExport.inputValue("B4", Util.FormatDate(Util.parseDateTime(phieu.Rows[0][PHIEU_NK_TP_CT_DTO.N_CREATION_TIME])));
                    excelExport.inputValue("E4", phieu.Rows[0][PHIEU_NK_TP_CT_DTO.N_MS_PHIEU_NK]);
                    int startIndex = 6, count = 0, curIndex = 0;
                    foreach (DataRow curRow in phieu.Rows)
                    {
                        curIndex = startIndex + count;

                        excelExport.inputValue(new string[] { "A" + curIndex, "A" + curIndex }, curRow[PHIEU_NK_TP_CT_DTO.N_MA_MAU], false);
                        excelExport.inputValue(new string[] { "B" + curIndex, "B" + curIndex }, curRow[PHIEU_NK_TP_CT_DTO.N_SO_LO], false);
                        excelExport.inputValue(new string[] { "C" + curIndex, "C" + curIndex }, curRow[PHIEU_NK_TP_CT_DTO.N_DON_VI], false);
                        excelExport.inputValue(new string[] { "D" + curIndex, "D" + curIndex }, curRow[PHIEU_NK_TP_CT_DTO.N_SL_THUC_NHAP], false);
                        excelExport.inputValue(new string[] { "E" + curIndex, "E" + curIndex }, curRow[PHIEU_NK_TP_CT_DTO.N_GHI_CHU], false);
                        count++;
                    }

                    b = excelExport.saveExcel(newPath);
                    excelExport.close();
                }
            }
            return b;
        }
        #endregion
    }
}
